package com.poker.controller;

import com.poker.consts.Consts;
import com.poker.entity.ManagerInfo;
import com.poker.util.Misc;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class BaseController {

    String redirect(String action) {
        return "redirect:" + action;
    }

    String managerPage(String page) {
        return getString("manager/", page);
    }

    String toJson(boolean success, String message, Object data) {
        return Misc.toJson(success, message, data);
    }

    ManagerInfo loginManager() {
        return (ManagerInfo)getObjectFromShiroSession(Consts.Session.MANAGER);
    }

    Session getShiroSession() {
        return  SecurityUtils.getSubject().getSession();
    }

    Object getObjectFromShiroSession(String key) {
        return getShiroSession().getAttribute(key);
    }

    void addToShiroSession(String key, Object value) {
        getShiroSession().setAttribute(key, value);
    }

    String getStringFromShiroSession(String key) {
        Object obj = getObjectFromShiroSession(key);
        return (null == obj) ? null : obj.toString();
    }

    ManagerInfo getLoginManager () {
        return (ManagerInfo) getObjectFromShiroSession(Consts.Session.MANAGER);
    }

    void toWeb(HttpServletResponse response, Object object) {
        try {
            response.setContentType("text/html;charset=UTF-8");
            PrintWriter objPw = response.getWriter();

            objPw.print(object);
            objPw.flush();
            objPw.close();
        } catch (IOException e) {
            e.getMessage();
        }
    }

     Integer getManagerInfo(){
        ManagerInfo managerInfo = loginManager();
        Integer activityClassify;
        if(managerInfo.getUserName().equals("admin")){
            activityClassify=null;
        }else{
            activityClassify = managerInfo.getClassify();
        }
        return activityClassify;
    }
    private String getString(String... strs) {
        StringBuffer buf = new StringBuffer();
        if (null != strs && strs.length > 0) {
            for (String str : strs) { buf.append(str); }
        }
        return buf.toString();
    }
}
